package com.koron.css2.serviceManage.impl;

import com.koron.common.web.bean.DepartmentTreeBean;
import com.koron.common.web.mapper.DepartmentMapper;
import com.koron.common.web.mapper.LongTreeBean;
import com.koron.common.web.mapper.TreeMapper;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TAuthorizationBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.TAuthorizationMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class TAuthorizationList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);

		try {
			TAuthorizationBean bean = JsonUtils.objectToPojo(req.getData(), TAuthorizationBean.class);

			DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
			TreeMapper tmapper = factory.getMapper(TreeMapper.class);
			// 目的:获取选中部门下所有授权的人员以及部门
			// 1获取子部门列表
			// 1.1 查通过departmentid查询所有子部门信息
			LongTreeBean longTreeBean = tmapper.getByForeignkey(Integer.valueOf(bean.getDepartment()));
			List<DepartmentTreeBean> department = mapper.getDescendantByParentId(longTreeBean.getSeq());
			// 1.2部门code信息，加入查询列表中
			List<String> departmentIds = new ArrayList<>();
			for (DepartmentTreeBean departmentTreeBean : department) {
				departmentIds.add(departmentTreeBean.getCode());
			}
			TAuthorizationMapper tAuthorizationMapper = factory.getMapper(TAuthorizationMapper.class);
			// 列表查询
			List<TAuthorizationBean> list = tAuthorizationMapper.selectTAuthorizationByName(departmentIds, bean.getUserName(),
					bean.getCompanyCode());
			info.setData(list);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
